package cn.rui.service;

import cn.rui.mapper.UserMapper;
import cn.rui.pojo.User;
import cn.rui.utils.SaltUtils;
import cn.rui.vo.UserQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 徽州大都督
 * @date 2020/8/15
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 会员添加
     *
     * @param user
     * @return
     */
    public int register(User user) {

        String salt = SaltUtils.getSalt (8);
        Md5Hash md5Hash = new Md5Hash (user.getPassword (), salt, 1024);
        user.setPassword (md5Hash.toHex ());
        if (user.getSexStr ().equals ("男")) {
            user.setSex (1);
        } else {
            user.setSex (0);
        }
        user.setSalt (salt);
        user.setIntegral (0L);
        user.setMoney (0.0);
        int insert = userMapper.insert (user);
        return insert;
    }

    /**
     * 分页查询用户
     *
     * @param page
     * @param size
     * @return
     */
    public List<User> findByPage(int page, int size, UserQuery userQuery) {

        Page<User> page1 = new Page<> (page, size);

        QueryWrapper<User> wrapper =new QueryWrapper<> ();
        if (userQuery.getTime ()!=null&&userQuery.getUsers ()!=null){
            wrapper.inSql ("username","select username from user where create_time like '"+"%"+userQuery.getTime ()+"%'");
            wrapper.like ("username",userQuery.getUsers ());
        }

        if (userQuery.getUsers ()==null&&userQuery.getTime ()!=null){
            wrapper.like ("create_time",userQuery.getTime ());
        }

        if (userQuery.getUsers ()!=null &&userQuery.getTime ()==null){
            wrapper.like ("username",userQuery.getUsers ());
        }

        IPage<User> userIPage = userMapper.selectPage (page1, wrapper);
        long total = userIPage.getTotal ();

        if (total==0){
            Page<User> page2 = new Page<> (page, size);

            QueryWrapper<User> wrapper2 =new QueryWrapper<> ();
            if (userQuery.getTime ()!=null&&userQuery.getUsers ()!=null){
                wrapper2.inSql ("email","select email from user where create_time like '"+"%"+userQuery.getTime ()+"%'");
                wrapper2.like ("email",userQuery.getUsers ());
            }

            if (userQuery.getUsers ()==null&&userQuery.getTime ()!=null){
                wrapper2.like ("create_time",userQuery.getTime ());
            }

            if (userQuery.getUsers ()!=null &&userQuery.getTime ()==null){
                wrapper2.like ("email",userQuery.getUsers ());
            }

            IPage<User> userIPage1 = userMapper.selectPage (page2, wrapper2);
            long total1 = userIPage1.getTotal ();
            if (total1==0){

                Page<User> page3 = new Page<> (page, size);

                QueryWrapper<User> wrapper3 =new QueryWrapper<> ();
                if (userQuery.getTime ()!=null&&userQuery.getUsers ()!=null){
                    wrapper3.inSql ("telephone","select telephone from user where create_time like '"+"%"+userQuery.getTime ()+"%'");
                    wrapper3.like ("telephone",userQuery.getUsers ());
                }

                if (userQuery.getUsers ()==null&&userQuery.getTime ()!=null){
                    wrapper3.like ("create_time",userQuery.getTime ());
                }

                if (userQuery.getUsers ()!=null &&userQuery.getTime ()==null){
                    wrapper3.like ("telephone",userQuery.getUsers ());
                }
                IPage<User> userIPage2 = userMapper.selectPage (page3, wrapper3);

                long total2 = userIPage2.getTotal ();

                if (total2==0){
                    List<User> list2=new ArrayList<> ();

                    return list2;
                }else {
                    List<User> list = userIPage2.getRecords ();
                    for (User user : list) {
                        user.setTotal (total2);
                    }
                    return list;
                }


            }else {
                List<User> userList2 = userIPage1.getRecords ();
                for (User user : userList2) {
                    user.setTotal (total1);
                }
                return userList2;
            }


        }else {
            List<User> userList = userIPage.getRecords ();
            for (User user : userList) {
                user.setTotal (total);
            }

            return userList;
        }


    }

    /**
     * 修改用户状态
     *
     * @param uid
     */
    public void updateStutas(Integer uid) {
        User user = userMapper.selectById (uid);

        if (user.getStatus () == 1) {
            user.setStatus (0);
        } else {
            user.setStatus (1);
        }

        userMapper.updateById (user);
    }

    /**
     * 删除用户
     *
     * @param uid
     */
    public void deleteUser(Integer uid) {
        userMapper.deleteById (uid);
    }

    /**
     * 根据uid查询
     *
     * @param uid
     * @return
     */
    public User findById(Integer uid) {

        return userMapper.selectById (uid);
    }



    /**
     * 批量删除会员
     * @param users
     */
    public void delAll(List<Integer> users) {
        for (Integer uid : users) {
            userMapper.deleteById (uid);
        }
    }
}
